﻿using AutoMapper;
using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.AccountbalanceCommand;
using MediatR;

namespace D.Medicalcare.Write.API.Application.Handlers.AccountbalanceHandler
{
    public class CreateAccountbalanceHandler : IRequestHandler<CreateAccountbalanceCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Accountbalance> accRepo;
        private readonly IMapper mapper;

        public CreateAccountbalanceHandler(IBaseRepository<Accountbalance> accRepo, IMapper mapper)
        {
            this.accRepo = accRepo;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(CreateAccountbalanceCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<Accountbalance>(request);
            model.Status  = "正常";
            model.IsDeleted = false;
            var s = accRepo.AddAsync(model).Result;
            if (s > 0)
            {
                result.Code = APIEnum.Success;
                result.Msg = "添加成功";
                result.Data = s;
            }
            else
            {
                result.Code = APIEnum.Failed;
                result.Msg = "添加失败";
                result.Data = s;
            }
            return Task.FromResult(result);
        }
    }
}
